local skill = fk.CreateSkill {
  name = "#ling__warship_skill",
  attached_equip = "ling__warship",
}

skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player.dead or not self:isEffectable(player) then return end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerEquip then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).name == skill.attached_equip then
            return true
          end
        end
      end
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).name == skill.attached_equip then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerEquip then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).name == skill.attached_equip then
            room:changeShield(player, 3)
          end
        end
      end
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).name == skill.attached_equip then
            room:changeShield(player, -3)
          end
        end
      end
    end
  end,
})

skill:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return table.find(player:getCardIds("e"), function (id)
      return Fk:getCardById(id).name == skill.attached_equip
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    for _, move in ipairs(data) do
      if move.from == player and move.toArea ~= Card.Void then
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if Fk:getCardById(id).name == skill.attached_equip and info.fromArea == Card.PlayerEquip then
            if not player.dead and player:getMark(skill.name) == 0 then
              room:setPlayerMark(player, skill.name, 1)
              table.insert(ids, id)
            end
          end
        end
      end
    end
    if #ids > 0 then
      room:cancelMove(data, ids)
    end
  end,
})

skill:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(skill.name) then
      return 3
    end
  end,
})

return skill
